#include "dllib.h"

#include <stdio.h>
#include <dlfcn.h>
#include <string>


DlLib::DlLib() {
	Open = NULL;
}

DlLib::~DlLib() {
	if( Open != NULL )
		dlclose( Open );
}

bool DlLib::Init( const char *name )
{
	std::string path;
	
#ifdef DLLIB_DEBUG
// 	LDEBUG("DLIB\tdlopen [%s]", name );
#endif

	path = "lib" + std::string( name ) + ".so";
	
	Open = dlopen( path.c_str(), RTLD_LAZY );
	if( Open == NULL )
	{
		printf("DLIB\tFallo dlopen [%s] dlerror[%s]\n", name, dlerror() );
		return false;
	}
	
	create = (dllib_create_t *)dlsym(Open, "dl_create");
	if( create == NULL )
	{
		printf("DLIB\tFalló dlsym [%s] dl_create dlerror[%s]\n", name, dlerror() );
		return false;
	}
	
	destroy = (dllib_destroy_t *)dlsym(Open, "dl_destroy");
	if( destroy == NULL )
	{
		printf("DLIB\tFalló dlsym [%s] dl_destroy dlerror[%s]\n", name, dlerror() );
		return false;
	}

	printf("DLIB\tCarga exitosa [%s]\n", path.c_str() );
	return true;
}

void *DlLib::Create( void **args )
{ 
	return create( args );
}

void DlLib::Destroy( void *plg )
{
	return destroy( plg );
}
